﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BadassPhotoAlbum
{
    public partial class AlbumWizard : Form
    {
        private Interface theInterface; //interface pointer for exist checks
        private string albumName;
        private string albumDescription;
        private bool validFlag;
        private bool editFlag = false;
        private string editOriginalName;
        private const int NAME_COUNT = 20;      // max char count for an album name
        private const int DESCRIP_COUNT = 130;  // max char count for an album desciption

        /// <summary>
        /// Album wizard contstructor
        /// Written By: Steven Gonzalez & Adam Reaves
        /// </summary>
        public AlbumWizard(Interface inf)
        {
            InitializeComponent();
            theInterface = inf;

            validFlag = false;
            albumName = "";
            albumDescription = "";

            albumName = "";
            albumDescription = "";

            AlbumDescriptionTextBox.KeyDown += new KeyEventHandler(AlbumDescriptionTextBox_KeyDown);
            AlbumDescriptionTextBox.TextChanged += new EventHandler(AlbumDescriptionTextBox_TextChanged);
            AlbumNameTextBox.TextChanged += new EventHandler(AlbumNameTextBox_TextChanged);
            AlbumNameTextBox.KeyDown += new KeyEventHandler(AlbumNameTextBox_KeyDown);
            AlbumNameTooltip.SetToolTip(AlbumNameTextBox, "Enter A Unique Album Name");

        }

        /// <summary>
        /// Album wizard constructor that overrides the text for resase on edit
        /// Written By: Steven Gonzalez & Adam Reaves
        /// </summary>
        public AlbumWizard(Interface inf, string promptOverride,
            string curAlbumName, string curAlbumDesc) :
            this(inf)
        {
            this.NewAlbumLabel.Text = promptOverride;
            this.AlbumNameTextBox.Text = curAlbumName;
            this.AlbumDescriptionTextBox.Text = curAlbumDesc;
            editOriginalName = curAlbumName;
            editFlag = true;
        }

        /// <summary>
        /// Captures the keydown so that enter is an form ender
        /// Written By: Steven Gonzalez & Adam Reaves
        /// </summary>
        void AlbumNameTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    this.AcceptForm();
                    break;
            }
        }

        /// <summary>
        /// captures the the key so enter is a form ender
        /// Written By: Steven Gonzalez & Adam Reaves
        /// </summary>
        void AlbumDescriptionTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    this.AcceptForm();
                    break;
            }
        }

        /// <summary>
        /// This function keeps track of the album desciption length.
        /// A character counter is updated when the text count changes.
        /// Written By: Steven Gonzalez
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AlbumDescriptionTextBox_TextChanged(object sender, EventArgs e)
        {
            descripCharCounter.Text = "CHARS LEFT: " +
                (DESCRIP_COUNT - AlbumDescriptionTextBox.TextLength);
        }

        /// <summary>
        /// This function keeps track of the album name length.
        /// A character counter is updated when the text count changes.
        /// Written By: Steven Gonzalez
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AlbumNameTextBox_TextChanged(object sender, EventArgs e)
        {
            nameCharCounter.Text = "CHARS LEFT: " +
                (NAME_COUNT - AlbumNameTextBox.TextLength);
        }

        /// <summary>
        /// Called when the clear button is clicked
        /// Written By: Adam Reaves
        /// </summary>
        private void ClearButton_Click(object sender, EventArgs e)
        {
            this.ClearForm();
        }

        /// <summary>
        /// Called when the accept button is clicked
        /// Written By: Adam Reaves
        /// </summary>
        private void AcceptButton_Click(object sender, EventArgs e)
        {
            this.AcceptForm();
        }

        /// <summary>
        /// Called when the cancel button is clicked
        /// Written By: Adam Reaves & Steven Gonzalez
        /// </summary>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.CancelForm();
        }

        /// <summary>
        /// Called when the clear button is clicked
        /// clears all the text boxes
        /// Written by: Adam Reaves & Steven Gonzalez
        /// </summary>
        private void ClearForm()
        {
            AlbumDescriptionTextBox.Text = "";
            AlbumNameTextBox.Text = "";
        }

        /// <summary>
        /// This checks the form boxes and validates the information against the database
        /// To see if user entered information is correct
        /// Written By: Adam Reaves
        /// </summary>
        private void AcceptForm()
        {
            albumDescription = AlbumDescriptionTextBox.Text;
            albumName = AlbumNameTextBox.Text;

            if(albumName.Trim() == "")
            {
                AlbumNameTooltip.Show("Please Enter A Unique Album Name", AlbumNameTextBox, 0, 0, 3000);
                return;
            }

            if (editFlag && albumName == editOriginalName)
            {
                //cool.. let's do nothing!
            }
            else
            {
                if (theInterface.IsPresentAlbumByName(albumName))
                {
                    AlbumNameTooltip.Show("Please Enter A Unique Album Name", AlbumNameTextBox, 0, 0, 3000);
                    return;
                }
            }
            //gotten this far, must be vaid
            this.validFlag = true;

            this.Close();
        }

        /// <summary>
        /// Called when the cancel button is clicked.
        /// Cancels Album Wizard and returns to GUI.
        /// Written by: Steven Gonzalez
        /// </summary>
        private void CancelForm()
        {
            this.validFlag = false;
            this.Close();
        }

        /// <summary>
        /// Returns the Albums Name.
        /// Function returns a string name.
        /// Written by: Steven Gonzalez & Adam Reaves
        /// </summary>
        public string AlbumName
        {
            get { return albumName; }
        }

        /// <summary>
        /// Returns the Albums Description.
        /// Function returns a string description.
        /// Written by: Adam Reaves & Steven Gonzalez
        /// </summary>
        public string AlbumDescription
        {
            get { return albumDescription; }
        }

        /// <summary>
        /// Validates the Form.
        /// Function returns a boolean flag.
        /// Written by: Steven Gonzalez & Adam Reaves
        /// </summary>
        public bool Valid
        {
            get { return validFlag; }
        }
    }
}
